//
//  EeMimeFile.swift
//  Aiwei
//
//  Created by zhu xietong on 2017/12/1.
//  Copyright © 2017年 zhu xietong. All rights reserved.
//

import Foundation

public func FileName() ->String{
    TaskFile.index = TaskFile.index + 1
    let name =  "iosfile" + "\(UUID().uuidString)" + Date()["YYYYMMddHHmmss"] + "\(TaskFile.index)"
    return name
}

public protocol MimeFileSupport {
    var mimeName:String{get}
    var rawData:Data{get}
    var fileName:String{get}
}

public enum MimeImage:MimeFileSupport{
    case png(UIImage)
    case jpg(UIImage)
    public var mimeName:String{
        get{
            switch self {
            case .jpg:
                return "image/jpg"
            default:
                return "image/png"
            }
        }
    }
    
    public var fileName: String{
        get{
            switch self {
            case .jpg:
                return FileName() + ".jpg"
            default:
                return FileName() + ".png"
                
            }
        }
    }
    
    public var rawData:Data{
        get{
            switch self {
            case .jpg(let img):
                return img.jpegData(compressionQuality: 1)!
            case .png(let img):
                return img.pngData()!
//                return UIImagePNGRepresentation(img)!
            }
        }
    }
}

public enum MimeVideo:MimeFileSupport{
    
    case mp4(URL)
    case mov(URL)
    public var mimeName:String{
        get{
            switch self {
            case .mp4:
                return "video/mp4"
            default:
                return "video/quicktime"
            }
        }
    }
    
    public var fileName: String{
        get{
            switch self {
            case .mov:
                return FileName() + ".mov"
            default:
                return FileName() + ".mp4"
                
            }
        }
    }
    
    public var rawData:Data{
        get{
            switch self {
            case .mov(let url),.mp4(let url):
                do {
                    let data = try Data(contentsOf: url)
                    return data
                }catch{
                    return Data()
                }
            }
        }
    }
    
}

public enum MimeFile:MimeFileSupport{
    case image(MimeImage)
    case video(MimeVideo)
    case filePath(String)
    
    public var rawData: Data{
        get{
            switch self {
            case .image(let image):
                return image.rawData
            case .video(let video):
                return video.rawData
            case .filePath(let file_path):
                do {
                    let url = URL(fileURLWithPath: file_path)
                    let data = try Data(contentsOf: url)
                    return data
                }catch{
                    return Data()
                }
            }
        }
    }
    
    
    public var mimeName:String{
        get{
            switch self {
            case .image(let file):
                return file.mimeName
            case .video(let file):
                return file.mimeName
            case .filePath(let file_path):
                let cops = file_path.components(separatedBy: ".")
                let type = (cops.last ?? "png").lowercased()
                switch type {
                case "png":
                    let img = UIImage(contentsOfFile: file_path) ?? UIImage()
                    let file = MimeImage.png(img)
                    return file.mimeName
                case "jpg":
                    let img = UIImage(contentsOfFile: file_path) ?? UIImage()
                    let file = MimeImage.jpg(img)
                    return file.mimeName
                case "mp4":
                    let url = URL(fileURLWithPath: file_path)
                    return MimeVideo.mp4(url).mimeName
                case "mov":
                    let url = URL(fileURLWithPath: file_path)
                    return MimeVideo.mov(url).mimeName
                default:
                    return "image/png"
                }
            }
        }
    }
    
    public var fileName:String{
        get{
            switch self {
            case .image(let file):
                return file.fileName
            case .video(let file):
                return file.fileName
            case .filePath(let file_path):
                let cops = file_path.components(separatedBy: ".")
                let type = (cops.last ?? "png").lowercased()
                switch type {
                case "png":
                    let img = UIImage(contentsOfFile: file_path) ?? UIImage()
                    let file = MimeImage.png(img)
                    return file.fileName
                case "jpg":
                    let img = UIImage(contentsOfFile: file_path) ?? UIImage()
                    let file = MimeImage.jpg(img)
                    return file.fileName
                case "mp4":
                    let url = URL(fileURLWithPath: file_path)
                    return MimeVideo.mp4(url).fileName
                case "mov":
                    let url = URL(fileURLWithPath: file_path)
                    return MimeVideo.mov(url).fileName
                default:
                    return "image/png"
                }
            }
        }
    }
    
}

public struct TaskFile {
    
    public var mimeFile:MimeFile
    
    public init(mimeFile:MimeFile)
    {
        self.mimeFile = mimeFile
        
        self.fileName = mimeFile.fileName
        self.mimeType = mimeFile.mimeName
    }
    
    public init(file_path:String)
    {
        self.mimeFile = .filePath(file_path)
        self.fileName = mimeFile.fileName
        self.mimeType = mimeFile.mimeName
    }
    static var index = 0
    
    
    public var data: Data{
        get{
            return self.mimeFile.rawData
        }
    }
    public var fieldName: String = "file"
    public var fileName: String = "image.png"
    public var mimeType: String = "image/png"
    
}
